Completed
Push — master ( 369c90...324e29 )
by Michael
04:57
created

$.ajax   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 64
rs 8.6346

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Ajax Queue Plugin
3
 *
4
 * Homepage: http://jquery.com/plugins/project/ajaxqueue
5
 * Documentation: http://docs.jquery.com/AjaxQueue
6
 */
7
8
/**
9
10
 <script>
11
 $(function(){
12
    jQuery.ajaxQueue({
13
        url: "test.php",
14
        success: function(html){ jQuery("ul").append(html); }
15
    });
16
    jQuery.ajaxQueue({
17
        url: "test.php",
18
        success: function(html){ jQuery("ul").append(html); }
19
    });
20
    jQuery.ajaxSync({
21
        url: "test.php",
22
        success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
23
    });
24
    jQuery.ajaxSync({
25
        url: "test.php",
26
        success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
27
    });
28
});
29
 </script>
30
 <ul style="position: absolute; top: 5px; right: 5px;"></ul>
31
32
 */
33
/*
34
 * Queued Ajax requests.
35
 * A new Ajax request won't be started until the previous queued
36
 * request has finished.
37
 */
38
39
/*
40
 * Synced Ajax requests.
41
 * The Ajax request will happen as soon as you call this method, but
42
 * the callbacks (success/error/complete) won't fire until all previous
43
 * synced requests have been completed.
44
 */
45
46
47
(function ($) {
48
49
    var ajax = $.ajax;
50
51
    var pendingRequests = {};
52
53
    var synced = [];
54
    var syncedData = [];
55
56
    $.ajax = function (settings) {
57
        // create settings for compatibility with ajaxSetup
58
        settings = jQuery.extend(settings, jQuery.extend({}, jQuery.ajaxSettings, settings));
59
60
        var port = settings.port;
61
62
        switch (settings.mode) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
63
            case "abort":
64
                if (pendingRequests[port]) {
65
                    pendingRequests[port].abort();
66
                }
67
                return pendingRequests[port] = ajax.apply(this, arguments);
68
            case "queue":
69
                var _old = settings.complete;
70
                settings.complete = function () {
71
                    if (_old)
72
                        _old.apply(this, arguments);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
73
                    jQuery([ajax]).dequeue("ajax" + port);
74
                    ;
75
                };
76
77
                jQuery([ajax]).queue("ajax" + port, function () {
78
                    ajax(settings);
79
                });
80
                return;
81
            case "sync":
82
                var pos = synced.length;
83
84
                synced[pos] = {
85
                    error: settings.error,
86
                    success: settings.success,
87
                    complete: settings.complete,
88
                    done: false
89
                };
90
91
                syncedData[pos] = {
92
                    error: [],
93
                    success: [],
94
                    complete: []
95
                };
96
97
                settings.error = function () {
98
                    syncedData[pos].error = arguments;
99
                };
100
                settings.success = function () {
101
                    syncedData[pos].success = arguments;
102
                };
103
                settings.complete = function () {
104
                    syncedData[pos].complete = arguments;
105
                    synced[pos].done = true;
106
107
                    if (pos == 0 || !synced[pos - 1])
108
                        for (var i = pos; i < synced.length && synced[i].done; i++) {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
109
                            if (synced[i].error) synced[i].error.apply(jQuery, syncedData[i].error);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
110
                            if (synced[i].success) synced[i].success.apply(jQuery, syncedData[i].success);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
111
                            if (synced[i].complete) synced[i].complete.apply(jQuery, syncedData[i].complete);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
112
113
                            synced[i] = null;
114
                            syncedData[i] = null;
115
                        }
116
                };
117
        }
118
        return ajax.apply(this, arguments);
119
    };
120
121
})(jQuery);
122